package org.ajc.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.ajc.base.annotation.AjcPermission;
import org.ajc.base.util.AjaxResult;
import org.ajc.base.util.PageInfo;
import org.ajc.org.domain.Department;
import org.ajc.org.query.DepartmentQuery;
import org.ajc.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/department")
@Api(value = "部门管理接口",description = "部门管理")
@AjcPermission(name = "部门管理",desc = "部门相关接口")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;
    /**
        (/department 这个接口即是新增修改又是查询全部)
        /department      新增和修改 put
        /department/{id} 删除单个 delete
        /department/{id} 查询单个 get
        /department      查询全部 get
        /department      删除多个 patch
        ResultFul 风格 (这不是技术) 不在通过使用路径请求 而是通过请求方式
     */
    /**新增或修改*/
    @PutMapping
    @ApiOperation("部门新增和修改接口")
    @AjcPermission(name = "保存/修改",desc = "保存/修改相关接口")
    public AjaxResult save(@RequestBody Department department){
        try {
            if (Objects.isNull(department.getId())){
                //id为空 新增
                departmentService.insert(department);
            }else{
                //id不为空  修改
                departmentService.update(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**查询全部*/
    @GetMapping
    @ApiOperation("部门查询全部接口")
    @AjcPermission(name = "部门查询",desc = "查询所有")
    public AjaxResult selectAll(){
        try {
            List<Department> departments = departmentService.selectAll();
            return AjaxResult.success(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**查询指定*/
    @GetMapping("/{id}")
    @ApiOperation("根据指定id查询部门")
    @AjcPermission(name = "部门查询",desc = "根据指定id查询部门")
    public AjaxResult selectById(@PathVariable("id")String id){
        try {
            Department department = departmentService.selectById(id);
            if (Objects.nonNull(department)){
                return AjaxResult.success(department);
            }else{
                return AjaxResult.query(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**删除指定*/
    @DeleteMapping("/{id}")
    @ApiOperation("根据指定id删除部门")
    @AjcPermission(name = "部门删除",desc = "根据指定id删除部门")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
           departmentService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**删除多个*/
    @PatchMapping
    @ApiOperation("删除多个部门")
    @AjcPermission(name = "部门删除",desc = "删除多个部门")
    public AjaxResult batchDelete(@RequestBody List<Serializable> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**部门分页查询*/
    @PostMapping
    @ApiOperation("分页查询")
    @AjcPermission(name = "部门分页查询",desc = "部门分页查询")
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery){
        try {
            PageInfo pageInfo = departmentService.queryPage(departmentQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    /**查询上级部门*/
    @GetMapping("/deptTree")
    @ApiOperation("查询上级部门")
    @AjcPermission(name = "查询上级部门",desc = "查询上级部门")
    public AjaxResult deptTree(){
        try {
            List<Department> deptTree = departmentService.deptTree();
            if (Objects.nonNull(deptTree)){
                return AjaxResult.success(deptTree);
            }else{
                return AjaxResult.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
